package etlkettle.demo.controller;

import java.io.*;
// 扫描文件并上传
public class ETLScanFile {

    public static void main(String[] args) {
        String path ="D:\\kettle";
        System.out.println(getFiles(path));
    }
    public static String getFiles(String path){

        File rootDir = new File(path);

        if(!rootDir.isDirectory()){
            System.out.println("文件名"+rootDir.getAbsolutePath());
            try {
                FileInputStream inputStream = new FileInputStream(rootDir);

                String filepath = "./files/";
                File fileDir = new File(filepath);
                if (!fileDir.exists()){
                    fileDir.mkdirs();
                }

                File file = new File( filepath+rootDir.getName());
                if (!file.exists()){
                    file.createNewFile();

                }
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                byte[] bytes = new byte[1024];
                int read;
                while ( (read = inputStream.read(bytes))>0){
                    System.out.println(read);
                    fileOutputStream.write(bytes,0,read);
                }
                inputStream.close();
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            String[] fileList =  rootDir.list();

            for (int i = 0; i < fileList.length; i++) {
                path = rootDir.getAbsolutePath()+"\\"+fileList[i];      getFiles(path);
            }
        }      return null;
    }
}
